package com.jxm.spring.security.controller;


import com.jxm.spring.security.bean.User;
import com.jxm.spring.security.common.ResponseUtils;
import com.jxm.spring.security.security.domain.SysUserDetails;
import com.jxm.spring.security.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 系统用户 前端控制器
 * </p>
 *
 * @author jxm
 * @since 2021-05-16
 */

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 查询用户信息
     *
     * @return
     */
    @PreAuthorize(value = "hasPermission('/user/info', 'user:list')")
    @RequestMapping(value = "/info")
    public ResponseUtils info() {
        SysUserDetails sysUserDetails = (SysUserDetails) SecurityContextHolder.getContext().getAuthentication()
                .getPrincipal();
        User sysUser = userService.getById(sysUserDetails.getUserId());
        return ResponseUtils.success(sysUser);
    }

    @PreAuthorize(value = "hasAnyAuthority('user:list')")
    @RequestMapping(value = "/info1")
    public ResponseUtils info1() {
        SysUserDetails sysUserDetails = (SysUserDetails) SecurityContextHolder.getContext().getAuthentication()
                .getPrincipal();
        User sysUser = userService.getById(sysUserDetails.getUserId());
        return ResponseUtils.success(sysUser);
    }

}
